package com.exercise.ThreadAttribute;

/**
 * 终端状态
 */
public class ThreadInterruptExample {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            //当终端状态为false时
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("Thread is running");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("Thread interrupted!");
                    // 恢复中断状态, 终端状态设置为true
                    Thread.currentThread().interrupt();
                }
            }
        });
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
    }
}
